/*
  Created on Jul 6, 2005
  (C) 2005 
  @author Michael Long

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package snobject;

public class snObjectArgList {
	private snObject[] arglist;
	int i;
	
	public int length(){
		return arglist.length;
	}
	
		public snObjectArgList(snObject[] argv){
		assert(argv.length>0);
		arglist = new snObject[argv.length];
		System.arraycopy(argv,0,arglist,0,arglist.length);
		i = 0;
	}
			
	private void checkBounds() throws ArrayIndexOutOfBoundsException{
		if(!hasNext())
			throw new ArrayIndexOutOfBoundsException();
	}
	
	public boolean hasNext() {
		return (i<arglist.length);
	}
	
	public int index(){
		return i;
	}
	
	public snObject popObject() throws ArrayIndexOutOfBoundsException{
		checkBounds();
		snObject s = arglist[i];
		i++;
		return s;
	}
	
	public snObject objectAt(int index) throws ArrayIndexOutOfBoundsException{
		if(index>=arglist.length)
			throw new ArrayIndexOutOfBoundsException();
		return arglist[index];
	}
	
	/**
	 * pop a String from the arg list, expanding the MACROs as appopriate 
	 * 
	 * @return snString with macros expanded
	 * 
	 * @throws ClassCastException
	 * @throws ArrayIndexOutOfBoundsException
	 */
	public snString popString() throws ClassCastException, ArrayIndexOutOfBoundsException{
		return (snString)popObject();
	}
	
	public snInteger popInteger() throws ArrayIndexOutOfBoundsException, ClassCastException{
		snObject t = popObject();
		if(t == snNil.NIL)
			return new snInteger(0);
		return (snInteger)t;
	}

	public snDouble popDouble() throws ArrayIndexOutOfBoundsException, ClassCastException{
		snObject t = popObject();
		if(t == snNil.NIL)
			return new snDouble(0);
		return (snDouble)t;
	}

	public snImage popImage() throws ArrayIndexOutOfBoundsException, ClassCastException{
		return (snImage)popObject();
	}
	
	public snBoolean popBoolean() throws ArrayIndexOutOfBoundsException, ClassCastException{
		return (snBoolean)popObject();
	}
	
	public snCommand popCommand() throws ArrayIndexOutOfBoundsException, ClassCastException{
		return (snCommand)popObject();
	}

	public snPair popPair() throws ArrayIndexOutOfBoundsException, ClassCastException{
		return (snPair)popObject();
	}
	
	public snStruct popStruct() throws ArrayIndexOutOfBoundsException, ClassCastException{
		return (snStruct)popObject();
	}
	
	public snSensor popSensor() throws ArrayIndexOutOfBoundsException, ClassCastException{
		return (snSensor)popObject();
	}

}
